//
//  ForumViewController.m
//  MoXiProject
//
//  Created by 沈晨宇 on 2017/7/31.
//  Copyright © 2017年 周筱雯. All rights reserved.
//

#import "ForumViewController.h"
#import "ForumViewModel.h"
#import "AllForumCell.h"
#import "ReleaseForumViewController.h"
#import "ForumDetailsViewController.h"
@interface ForumViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView* forumTable;
@property (nonatomic,strong) ForumViewModel * forumViewModel;
@property(nonatomic,strong)UIView * tempView;
@end

@implementation ForumViewController

-(ForumViewModel *)forumViewModel{
    
    if (!_forumViewModel) {
        _forumViewModel = [[ForumViewModel alloc] init];
    }
    return _forumViewModel;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initNav];
    [self initUI];
    [self createTempView];
    [self setupRefresh];

    // Do any additional setup after loading the view.
}
-(void)createTempView{
    
    self.tempView = [[UIView alloc]initWithFrame:CGRectMake(0, 0,DEF_DEVICE_WIDTH, DEF_DEVICE_HEIGHT-49-64)];
    
    UIImageView *imageview = [[UIImageView alloc]initWithFrame:CGRectMake(DEF_DEVICE_WIDTH/2-25, (CGRectGetHeight(self.tempView.frame) - 154)/2, 50, 50)];
    imageview.image = [UIImage imageNamed:@"wushuju"];
    imageview.contentMode = UIViewContentModeScaleAspectFit;
    [self.tempView addSubview:imageview];
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0,imageview.height+10 + imageview.y, CGRectGetWidth(self.tempView.frame), 30)];
    label.textColor = DEF_COLOR_RGB(215, 215, 215);
    label.textAlignment = NSTextAlignmentCenter;
    label.font = [UIFont systemFontOfSize:15.f];
    label.text = @"快来发布第一条论坛吧~";
    [self.tempView addSubview:label];
    
    [self.forumTable addSubview:self.tempView];
    self.tempView.hidden = YES;
}

-(void)setupRefresh{
    
    
    self.forumTable.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(mj_headerRefresh)];
    [self.forumTable.mj_header beginRefreshing];
    
        self.forumTable.mj_footer =[MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(mj_footerRefresh)];
    

}

-(void)mj_headerRefresh{
    
    self.forumViewModel.page=0;
    [self getForum];
    
}

-(void)mj_footerRefresh{
    
    [self getForum];
    
}

-(void)getForum{
    
//    [MXUtility showMBProgress:self.view message:LoadingText];
    
    [[self.forumViewModel forumListRequestParameter:LoadData] subscribeNext:^(id result) {
        
        if([result[@"errorcode"] integerValue] == 0){
            
            if (self.forumViewModel.allForumArrayList.count==0) {
                
                self.tempView.hidden = NO;
            }else{
                
                self.tempView.hidden = YES;
            }
            [self.forumTable reloadData];
        }
//        [MXUtility hideMBProgress:self.view];
        [self.forumTable.mj_header endRefreshing];
        [self.forumTable.mj_footer endRefreshing];
        
    }error:^(NSError *error) {
        
//        [MXUtility hideMBProgress:self.view];
        [self.forumTable.mj_header endRefreshing];
        [self.forumTable.mj_footer endRefreshing];
        
    }];
    
}


-(void)initNav{
    
    [self.navigationItem setTitle:@"大众论坛"];
    [self.view setBackgroundColor:baseWhiteColor];
    
    UIButton* rightButton= [[UIButton alloc] initWithFrame:CGRectMake(14, 15, 32, 32)];
    [rightButton setImage:[UIImage imageNamed:@"fabu_"] forState:UIControlStateNormal];
    [rightButton setImage:[UIImage imageNamed:@"fabu_"] forState:UIControlStateHighlighted];
    [rightButton addTarget:self action:@selector(fabu) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    
}

-(void)fabu{

    if (UserInfoShare.isLogin==YES) {
        
        ReleaseForumViewController * vc = [[ReleaseForumViewController alloc] init];
        vc.backBlock = ^(void){
        
        [self setupRefresh];
            
        };
        [self.navigationController pushViewController:vc animated:YES];

    }else{
        
     [MXUtility showTips:@"请先前去登录"];
    
    }
    
}

-(void)initUI{
    
    self.forumTable=[UITableView new];
    self.forumTable.delegate=self;
    self.forumTable.dataSource=self;
    self.forumTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.forumTable];
    
    if(iPhoneX){
        [self.forumTable mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(DEF_DEVICE_WIDTH);
            make.height.mas_equalTo(DEF_DEVICE_HEIGHT-88-49-34);
            make.top.equalTo(self.view).with.offset(0);
            make.leading.equalTo(self.view).with.offset(0);
        }];
        
    }else{
        
        [self.forumTable mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(DEF_DEVICE_WIDTH);
            make.height.mas_equalTo(DEF_DEVICE_HEIGHT-64-49);
            make.top.equalTo(self.view).with.offset(0);
            make.leading.equalTo(self.view).with.offset(0);
        }];
    }
    
   
    
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return self.forumViewModel.allForumArrayList.count;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return [self tableView:tableView cellForRowAtIndexPath:indexPath].frame.size.height;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString *CellIdentifier = @"forumCell";
    AllForumCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        
        cell = [[AllForumCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    cell.model=self.forumViewModel.allForumArrayList[indexPath.row];
    [cell.report addTarget:self action:@selector(report) forControlEvents:UIControlEventTouchUpInside];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    AllForumModel * model = [self.forumViewModel.allForumArrayList objectAtIndex:indexPath.row];
    
    ForumDetailsViewController * vc = [[ForumDetailsViewController alloc] init];
    vc.forumId=model.forumId;
    [self.navigationController pushViewController:vc animated:YES];
}
-(void)report{
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"是否举报这条帖子？" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *sure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction* action){
        
        [MXUtility showMBProgress:self.view message:LoadingText];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            [MXUtility hideMBProgress:self.view];
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"举报成功" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *sure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction* action){
                
            }];
            [alert addAction:sure];
            [self presentViewController:alert animated:YES completion:nil];
            
        });
        
    }];
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction* action){
        
    }];
    [alert addAction:cancel];
    [alert addAction:sure];
    [self presentViewController:alert animated:YES completion:nil];
    
}

@end
